<template>
  <div class="app-container notice-dialog">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="通知标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入通知标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="操作人员" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入操作人员"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="noticeType">
        <el-select
          v-model="queryParams.noticeType"
          placeholder="公告类型"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-row v-for="item in noticeList" :key="item.id" class="list-row">
        <el-col :span="24">
          <el-row class="title">
            <el-col
              :span="20"
              @click="goDetail(item)"
              style="cursor: pointer"
              >{{ item.title }}</el-col
            >
            <el-col :span="4">
              <el-button
                link
                type="primary"
                icon="View"
                @click="goDetail(item)"
                >查看详情</el-button
              >
            </el-col>
          </el-row>

          <el-row class="intro" :gutter="28"
            ><el-col :span="6">发布时间：{{ item.createTime }}</el-col
            ><el-col :span="4">发布人：{{ item.userName }}</el-col></el-row
          >
        </el-col>
      </el-row>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageDomain.pageNum"
      v-model:limit="queryParams.pageDomain.pageSize"
      @pagination="getList"
    />
    <el-dialog title="通知详情" v-model="dialogVisible" width="880px">
      <el-row>
        <el-col :span="24" class="notice-title">
          {{ currentNotice.title }}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div v-html="currentNotice.introduction" class="ql-editor notice-content"></div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" class="notice-form-title">相关附件</el-col>
      </el-row>
      <el-row
        v-for="(item, index) in currentNotice.fileList"
        :key="'infomation' + index"
        class="information-list"
      >
        <el-col :span="18">{{ item.name }}</el-col>
        <el-button
          :span="3"
          link
          type="primary"
          icon="Download"
          @click="download(item)"
          >下载</el-button
        >

        <el-button
          :span="3"
          link
          type="primary"
          icon="Reading"
          @click="preview(item)"
          >在线预览</el-button
        >
      </el-row>
    </el-dialog>
  </div>
  <PreviewDialog
    v-model="fileDialogVisible"
    :infor="currentInfor"
  ></PreviewDialog>
</template>

<script setup name="Notice">
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/system/notice";
import { getNoticeList, getNoticeInfo } from "@/api/pms/project";
import PreviewDialog from "./previewDialog.vue";
const props = defineProps({
  projectId: {
    type: String,
  },
});
const route = useRoute();
const router = useRouter();

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogVisible = ref(false);
const currentNoticeId = ref("");
const currentNotice = ref({});
const currentInfor = ref({});
const fileDialogVisible = ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageDomain: {
      isAsc: "true",
      orderByColumn: "",
      pageNum: 1,
      pageSize: 10,
      reasonable: true,
    },
    proId: "",
    title: "",
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  getNoticeList(queryParams.value).then((response) => {
    noticeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageDomain.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

//打开详情
function goDetail(item) {
  getNoticeInfo({ id: item.id }).then((response) => {
    currentNotice.value = response.data;
    dialogVisible.value = true;
  });
}

/** 下载文件 */
function download(item) {
  proxy.download(
    "/common/download",
    {
      fileName: item.url.replace("/profile/upload", ""),
      delete: false,
    },
    item.name
  );
}
//打开在线预览弹窗
function preview(item) {
  currentInfor.value = item;
  fileDialogVisible.value = true;
}
watch(
  () => props.projectId,
  (newId, oldId) => {
    if (newId) {
      queryParams.value.proId = props.projectId;
      getList();
    }
  },
  { deep: true, immediate: true }
);
</script>

<style scoped lang="scss">
.list-row {
  padding: 10px 0;
  margin: 10px 0;
  border-bottom: 1px dashed #cccccc;
  .title {
    padding: 10px 0;
  }
  .intro {
    color: #cccccc !important;
  }
}
.label-box {
  display: flex;
  color: #cccccc !important;
  margin: 10px 0;
  .label {
    border-radius: 5px;
    border: 1px solid var(--el-color-primary);
    padding: 2px 5px;
    margin: 0 5px;
    color: var(--el-color-primary);
    background: rgba(174, 212, 255, 0.1);
  }
}
</style>
<style lang="scss">
.notice-content {
  white-space: pre-wrap;
  img {
    width: 100%;
    height: auto;
  }
}
.notice-dialog {
  .el-dialog {
    .el-dialog__body {
      height: 80vh;
      overflow-y: auto;
      .notice-form-title {
        margin: 20px 0;
        border-left: 3px solid rgb(64, 158, 255);
        padding: 0 10px;
        font-weight: bold;
      }
      .notice-title {
        color: rgb(64, 158, 255);
        font-size: 18px;
        font-weight: bold;
        text-align: center;
      }
    }
  }
}
</style>